Repository navigation
Fix folds that wrap an inner fold, plus RingArray and Serialize leftovers - #3257
Merged
Merged
Conversation
When the requested range did not wrap around the end of the ring, copyToMutArray still copied from the start index to the end of the ring into an array of the requested length, writing past its end, and then copied a negative number of elements. copyToMutArray 0 2 on a full 5-element ring failed an assertion in debug builds and corrupted memory otherwise.
With encodeConstrNames, the derived deserializeAt compared the bytes of the constructor names with the buffer without checking that the name length read from the buffer fits in it. With encodeRecordFields, it compared the record header with the buffer without checking that the header fits in the buffer. A truncated or corrupt buffer made it read beyond the end of the buffer. When the constructor name does not fit, raise an error. When the record header does not fit, take the slow path that reads the header field by field with bounds checks.
The replace error message named insert, the eqArray error message named eqArrayN, and the moveReverse doc example defined moveForward.
When a Nothing (foldMaybes) or a Left (foldEithers) input terminated the fold, the state of the inner fold was dropped without calling its final. A resource released in final, e.g. by finallyIO or bracketIO, was not released until GC, or never for a fold with a plain effectful final. For example, folding [Just 1, Nothing] with foldMaybes over a fold whose final writes an IORef returned Nothing without writing the IORef.
refold used the deprecated extractM to get the initial value of the Refold, calling the source fold's extract instead of its final. The source fold was never finalized, and refold failed for folds whose extract is an error, e.g. splitWith, split_, concatMap, longest and duplicate. For example, refold Refold.sconcat over splitWith (++) (fromPure [1]) (take 1 (foldl' (++) [])) failed with "splitWith: cannot be used for scanning" instead of folding [[2]] to [1,2].
The definitions in the docs used Data.Foldable's foldl' and Prelude's foldr, which define functions on Foldable containers, not folds.
bracketIO stored the result of the inner fold's step, Partial or Done, and always returned Partial. Done was returned only on the next input, which was dropped: Stream.foldBreak with bracketIO over Fold.take 2 on [1..5] left [4,5] instead of [3,4,5]. If the stream ended right after the inner fold terminated, final returned the result without running the release action, which then ran only on GC.
finallyIO ran the action only in final. When the inner fold returned Done from initial or step, e.g. Fold.take 1 on [1,2,3], the action ran only when the IORef holding it was garbage collected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
More fixes from the review pass, mostly folds that wrap an inner fold, plus a few RingArray and Serialize leftovers.
Folds:
bracketIOsignalled Done one input late and dropped that input, e.g.foldBreakovertake 2on[1..5]left[4,5]instead of[3,4,5]. If the stream ended right there, the release action only ran on GC.finallyIOonly ran its action on GC when the inner fold finished early.foldMaybes/foldEithersdropped the inner fold's state without calling itsfinal.refoldused the deprecatedextractM, so the source fold was never finalized and folds without anextract(e.g.splitWith) just errored.toStreamK/toStreamKRev.RingArray / Serialize:
RingArray.Generic.copyToMutArraywrote past the end of the target array when the range didn't wrap around.deserializeAtcould read beyond the buffer on truncated input (constructor names / record headers weren't bounds checked).moveReversedoc example.Added tests for the fold and RingArray fixes; changelog entries for
bracketIOandfinallyIO.